package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/login")
    @ResponseBody
    public JsonResult login(String username, String password, HttpSession session){
        Employee employee = employeeService.login(username, password);
        if (employee != null){
            session.setAttribute("EMPLOYEE_IN_SESSION", employee);
            return new JsonResult(true, "登录成功");
        }
        return new JsonResult(false, "账号密码错误");
    }

    @RequestMapping("/logout")
    public String logout( HttpSession session){
        session.removeAttribute("EMPLOYEE_IN_SESSION");
        return "redirect:/static/login.html";
    }

    @RequestMapping("/noPermission")
    public String noPermission( HttpSession session){
        return null; //TODO
    }
}
